﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Du_Lich
{
    public partial class DangNhapForm : Form
    {
        public DangNhapForm()
        {
            InitializeComponent();
        }
        public static string manv;

        MainForm f = new MainForm(0);
      
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
           

            string strConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString;
            SqlConnection conn = null;
            conn = new SqlConnection(strConn);
            string strKiemTra = "select MaNV,UserName,PassWord,MaQuyen from NhanVien where UserName=@TenUser and PassWord=@MatKhau";
            SqlCommand cmd = new SqlCommand(strKiemTra, conn);

            cmd.Parameters.AddWithValue("@TenUser", txtTenDangNhap.Text);
            cmd.Parameters.AddWithValue("@MatKhau", txtMatKhau.Text);

            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            if(reader.Read()==true)
            {
                this.Hide();
                f = new MainForm(1);
                manv = reader["MaNV"].ToString();

                if(reader["MaQuyen"].ToString()=="1")
                {
                    f.MenuGiamDoc.Enabled = true;
                    f.MenuPhongNhanSu.Enabled = true;
                    f.MenuPhongKinhDoanh.Enabled = true;
                    f.MenuPhongKeHoach.Enabled = true;
                    f.MenuPhongKeToan.Enabled = true;
                    f.ShowDialog();
                    this.Close();
                }
               
               if(reader["MaQuyen"].ToString()=="2")
               {
                   f.MenuGiamDoc.Enabled = false;
                   f.MenuPhongNhanSu.Enabled = true;
                   f.MenuPhongKinhDoanh.Enabled = false;
                   f.MenuPhongKeHoach.Enabled = false;
                   f.MenuPhongKeToan.Enabled = false;
                   f.ShowDialog();
                   this.Close();
               }

               if (reader["MaQuyen"].ToString() == "3")
               {
                   f.MenuGiamDoc.Enabled = false;
                   f.MenuPhongNhanSu.Enabled = false;
                   f.MenuPhongKinhDoanh.Enabled = true;
                   f.MenuPhongKeHoach.Enabled = false;
                   f.MenuPhongKeToan.Enabled = false;
                   f.ShowDialog();
                   this.Close();
               }

               if (reader["MaQuyen"].ToString() == "4")
               {
                   f.MenuGiamDoc.Enabled = false;
                   f.MenuPhongNhanSu.Enabled = false;
                   f.MenuPhongKinhDoanh.Enabled = false;
                   f.MenuPhongKeHoach.Enabled = true;
                   f.MenuPhongKeToan.Enabled = false;
                   f.ShowDialog();
                   this.Close();
               }

               if (reader["MaQuyen"].ToString() == "5")
               {
                   f.MenuGiamDoc.Enabled = false;
                   f.MenuPhongNhanSu.Enabled = false;
                   f.MenuPhongKinhDoanh.Enabled = false;
                   f.MenuPhongKeHoach.Enabled = false;
                   f.MenuPhongKeToan.Enabled = true;
                   f.ShowDialog();
                   this.Close();
               }
            }
            else
                MessageBox.Show("Bạn Nhập sai Pass");
            conn.Close();
            this.Close();
        }  
    }
}
